# CMake 最低版本要求
cmake_minimum_required (VERSION 3.23.0)

# 项目名
project(Alice)

# 设置 Debug 模式
set(CMAKE_BUILD_TYPE "Debug")
# 设置可执行程序输出目录
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY bin)
# 设置项目使用的 CPP 版本
set(CMAKE_CXX_STANDARD 17)
# 设置第三方库下载地址
# glad
set(
    3RD_PARTY_GLAD
    URL https://github.com/zhtsu/3rdParty/releases/download/glad/glad.zip
)
# GLFW
set(
    3RD_PARTY_GLFW
    URL https://github.com/zhtsu/3rdParty/releases/download/GLFW/GLFW.zip
)
# spdlog
set(
    3RD_PARTY_SPDLOG
    URL https://github.com/gabime/spdlog/archive/refs/tags/v1.10.0.zip
)
# stb_image
set(
    3RD_PARTY_STB_IMAGE
    URL https://github.com/zhtsu/3rdParty/releases/download/stb_image/stb_image.zip
)
# glm
set(
    3RD_PARTY_GLM
    URL https://github.com/g-truc/glm/releases/download/0.9.9.8/glm-0.9.9.8.zip
)
# tinyXML2
set(
    3RD_PARTY_TINYXML2
    URL https://github.com/leethomason/tinyxml2/archive/refs/tags/9.0.0.zip
)

# SFML
set(
    3RD_PARTY_SFML
    URL https://github.com/SFML/SFML/archive/refs/tags/2.5.1.zip
)

# 设置 GLFW 的参数
set(GLFW_BUILD_EXAMPLES OFF)
set(GLFW_BUILD_TESTS OFF)
set(GLFW_BUILD_DOCS OFF)
set(GLFW_INSTALL OFF)

# 设置 SFML 的参数
set(SFML_SHARED_LIBS OFF)
set(SFML_BUILD_NETWORK OFF)
set(SFML_BUILD_GRAPHICS OFF)
set(SFML_BUILD_WINDOW OFF)
set(SFML_BUILD_EXAMPLES OFF)
set(SFML_BUILD_DOC OFF)

# 引入下载工具
include(FetchContent)
# 依次下载第三方库
message("Downloading
    ${3RD_PARTY_GLAD}
    ${3RD_PARTY_GLFW}
    ${3RD_PARTY_SPDLOG}
    ${3RD_PARTY_STB_IMAGE}
    ${3RD_PARTY_GLM}
    ${3RD_PARTY_TINYXML2}
    ${3RD_PARTY_SFML}"
)
FetchContent_Declare(
    glad
    ${3RD_PARTY_GLAD}
)
FetchContent_Declare(
    GLFW
    ${3RD_PARTY_GLFW}
)
FetchContent_Declare(
    spdlog
    ${3RD_PARTY_SPDLOG}
)
FetchContent_Declare(
    stb_image
    ${3RD_PARTY_STB_IMAGE}
)
FetchContent_Declare(
    glm
    ${3RD_PARTY_GLM}
)
FetchContent_Declare(
    tinyXML2
    ${3RD_PARTY_TINYXML2}
)
FetchContent_Declare(
    SFML
    ${3RD_PARTY_SFML}
)

# 解压下载的压缩包
FetchContent_MakeAvailable(glad)
FetchContent_MakeAvailable(GLFW)
FetchContent_MakeAvailable(spdlog)
FetchContent_MakeAvailable(stb_image)
FetchContent_MakeAvailable(glm)
FetchContent_MakeAvailable(tinyXML2)
FetchContent_MakeAvailable(SFML)

# 添加头文件包含路径
include_directories(
    include
)

# 设置需要编译的源文件
file(GLOB ALL_SRC_FILES
    src/Main.cpp
    src/Alice/*.cpp
    src/Alice/ECS/*.cpp
    src/Alice/Window/*.cpp
    src/Alice/Resource/*.cpp
    src/Alice/Log/*.cpp
    src/Alice/Timer/*.cpp
    src/Alice/Systems/*.cpp
    src/Alice/Core/*.cpp
    src/Alice/Graphs/*.cpp
    src/Alice/Math/*.cpp
)

set(SRC_FILES
    ${ALL_SRC_FILES}
)

# 输出可执行文件
add_executable(Alice ${SRC_FILES})

# 链接第三方库
target_link_libraries(
    Alice
    glad
    glfw
    spdlog
    stb_image
    glm
    tinyxml2
    sfml-audio
)

# 构建完成后进行拷贝操作
add_custom_command(
    TARGET ${CMAKE_PROJECT_NAME}
    POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy_directory
        "${PROJECT_SOURCE_DIR}/assets"
        "$<TARGET_FILE_DIR:${CMAKE_PROJECT_NAME}>/assets"    
)
